package com.qiangesoft.image.utils;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * url工具类
 *
 * @author qiangesoft
 * @date 2024-04-29
 */
public class UrlHelper {

    /**
     * 正则表达式
     */
    private static final Pattern PATTERN = Pattern.compile("((http|https|ftp):\\/\\/)?([\\w\\-]+\\.)+[\\w\\-]+(:[0-9]+)?(/[\\w\\[\\]\\-.~!*'();:@&=+$,/?#%]*)");

    public static void main(String[] args) throws MalformedURLException {
        String content = "8.97 复制打开抖音，看看【三融在东帝汶的作品】找个没人的地方. 然后一直躺到下午  https://v.douyin.com:8087/i2eTcJ82/?id=111&name=fsfe g@O.kP NwS:/ 06/18 ";
        List<String> list = UrlHelper.findAll(content);
        for (String s : list) {
            System.out.println(s);
        }

        String one = UrlHelper.findOne(content);
        System.out.println(UrlHelper.getProtocol(one));
        System.out.println(UrlHelper.getHost(one));
        System.out.println(UrlHelper.getPort(one));
        System.out.println(UrlHelper.getPath(one));
        System.out.println(UrlHelper.getFile(one));
        System.out.println(UrlHelper.getAuthority(one));
        System.out.println(UrlHelper.getQuery(one));
        System.out.println(UrlHelper.getQueryParam(one));
    }

    /**
     * 匹配第一个网址url
     *
     * @param content
     * @return
     */
    public static String findOne(String content) {
        Assert.notNull(content, "content must be not null !");

        List<String> list = findAll(content);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 匹配所有网址url
     *
     * @param content
     * @return
     */
    public static List<String> findAll(String content) {
        Assert.notNull(content, "content must be not null !");

        List<String> list = new ArrayList<>();
        findAll(content, (matcher) -> {
            list.add(matcher.group(0));
        });
        return list;
    }

    private static void findAll(String content, Consumer<Matcher> consumer) {
        if (null != content) {
            Matcher matcher = PATTERN.matcher(content);
            while (matcher.find()) {
                consumer.accept(matcher);
            }
        }
    }

    /**
     * 获取协议
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getProtocol(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getProtocol();
    }

    /**
     * 获取主机和端口号
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getAuthority(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getAuthority();
    }

    /**
     * 获取主机
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getHost(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getHost();
    }

    /**
     * 获取端口
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static int getPort(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getPort() == -1 ? urlO.getDefaultPort() : urlO.getPort();
    }

    /**
     * 获取文件名
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getFile(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getFile();
    }

    /**
     * 获取路径
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getPath(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getPath();
    }

    /**
     * 获取查询参数
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getQuery(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getQuery();
    }

    /**
     * 获取查询参数
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    private static Map<String, String> getQueryParam(String url) {
        String regex = "(\\?|&+)(.+?)=([^&]*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        Map<String, String> paramMap = new HashMap<>();
        while (matcher.find()) {
            String key = matcher.group(2);
            String value = matcher.group(3);
            paramMap.put(key, value);
        }
        return paramMap;
    }

}
